package com.queclink.ycm.entity.data;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import com.queclink.ycm.common.utils.HexUtils;
import com.queclink.ycm.common.utils.StringUtil;


/**
 * @author jim.li
 * @email jim.li@queclink.com
 * @date 2021-12-24 17:30:00
 */
public class DataDel90 extends Data {

    public String HardwareVersion;
    public String BootloaderVersion;
    public String ModemFirmwareVersion;
    public String BLEBootloaderVersion;
    public String BLEFirmwareVersion;
    public String ECUFirmwareVersion;
    public String OBDFirmwareVersion;
    public String WirepasFirmwareVersion;
    public String WirepasMeshAPIVersion;
    public String MeshAPIVersion;
    public String PlatformVersion;
    public String CoprocessorFirmwareVersion;
    public String FirmwareVersion;
    public String WiFiFirmwareVersion;
    public String ModemHardwareVersion;

    public DataDel90() {
    }

    @JSONField(
            name = "HardwareVersion"
    )
    public String getVersion(JSONObject jsonObject) {
        StringBuffer sb = new StringBuffer();

        if (jsonObject.containsKey("BootloaderVersion")) {
            String bootloaderVersion = StringUtil.replaceRav(jsonObject.getString("BootloaderVersion"));
            String bootloaderVersionstr = StringUtil.padZeroesToTheLeft(bootloaderVersion, 6);
            sb.append(bootloaderVersionstr);
        }

        if (jsonObject.containsKey("FirmwareVersion")) {
            String firmwareVersion = StringUtil.replaceRav(jsonObject.getString("FirmwareVersion"));
            String firmwareVersionstr = StringUtil.padZeroesToTheLeft(firmwareVersion, 6);
            sb.append(firmwareVersionstr);
        }

        if (jsonObject.containsKey("HardwareVersion")) {
            String hardwareVersion = StringUtil.replaceRav(jsonObject.getString("HardwareVersion"));
            String hardwareVersionstr = StringUtil.padZeroesToTheLeft(hardwareVersion, 6);
            sb.append(hardwareVersionstr);
        }

        Integer mask = 0;

        if (jsonObject.containsKey("ModemFirmwareVersion")) {
            mask += 1;
            String modemFirmwareVersion = jsonObject.getString("ModemFirmwareVersion");
            String modemFirmwareVersionHexStr = "8008" + HexUtils.string2HexUTF8(modemFirmwareVersion);
            int length = modemFirmwareVersionHexStr.length();
            sb.append(HexUtils.intToHex(length, 2));
            sb.append(modemFirmwareVersionHexStr);
        }

        if (jsonObject.containsKey("BLEBootloaderVersion")) {
            mask += 2;
            String bleBootloaderVersion = StringUtil.replaceRav(jsonObject.getString("BLEBootloaderVersion"));
            String bleBootloaderVersionstr = StringUtil.padZeroesToTheLeft(bleBootloaderVersion, 6);
            sb.append("8009");
            sb.append(bleBootloaderVersionstr);

            String firmwareVersion2 = StringUtil.replaceRav(jsonObject.getString("FirmwareVersion"));
            String firmwareVersionstr2 = StringUtil.padZeroesToTheLeft(firmwareVersion2, 6);
            sb.append("800A");
            sb.append(firmwareVersionstr2);
        }

        if (jsonObject.containsKey("ECUFirmwareVersion")) {
            mask += 4;
            String ecuFirmwareVersion = jsonObject.getString("ECUFirmwareVersion");
            String ecuFirmwareVersionHexStr = "800F" + HexUtils.string2HexUTF8(ecuFirmwareVersion);
            int length = ecuFirmwareVersionHexStr.length();
            sb.append(HexUtils.intToHex(length, 2));
            sb.append(ecuFirmwareVersionHexStr);
        }

        if (jsonObject.containsKey("OBDFirmwareVersion")) {
            mask += 8;
            String obdFirmwareVersion = jsonObject.getString("OBDFirmwareVersion");
            String obdFirmwareVersionHexStr = "800C" + HexUtils.string2HexUTF8(obdFirmwareVersion);
            int length = obdFirmwareVersionHexStr.length();
            sb.append(HexUtils.intToHex(length, 2));
            sb.append(obdFirmwareVersionHexStr);
        }

        if (jsonObject.containsKey("WirepasFirmwareVersion")) {
            mask += 16;
            String wirepasFirmwareVersion = StringUtil.replaceRav(jsonObject.getString("WirepasFirmwareVersion"));
            String wirepasFirmwareVersionstr = StringUtil.padZeroesToTheLeft(wirepasFirmwareVersion, 6);
            sb.append("800D");
            sb.append(wirepasFirmwareVersionstr);

            String wirepasMeshAPIVersion = StringUtil.replaceRav(jsonObject.getString("WirepasMeshAPIVersion"));
            String wirepasMeshAPIVersionstr = StringUtil.padZeroesToTheLeft(wirepasMeshAPIVersion, 6);
            sb.append("800E");
            sb.append(wirepasMeshAPIVersionstr);
        }

        if (jsonObject.containsKey("PlatformVersion")) {
            mask += 32;
            String platformVersion = StringUtil.replaceRav(jsonObject.getString("PlatformVersion"));
            String platformVersionHexStr = "8010" + HexUtils.string2HexUTF8(platformVersion);
            int length = platformVersionHexStr.length();
            sb.append(HexUtils.intToHex(length, 2));
            sb.append(platformVersionHexStr);
        }

        if (jsonObject.containsKey("CoprocessorFirmwareVersion")) {
            mask += 64;
            String coprocessorFirmwareVersion = StringUtil.replaceRav(jsonObject.getString("CoprocessorFirmwareVersion"));
            String coprocessorFirmwareVersionHexStr = "8011" + HexUtils.string2HexUTF8(coprocessorFirmwareVersion);
            int length = coprocessorFirmwareVersionHexStr.length();
            sb.append(HexUtils.intToHex(length, 2));
            sb.append(coprocessorFirmwareVersionHexStr);
        }

//        sb.insert(0, HexUtils.intToHex(mask, 2));
        return sb.toString();

    }

    public String getDataID() {
        // data90
        return HexUtils.intToHex(90, 2);
    }

    @Override
    public String combine(JSONObject jsonObject) throws Exception {
        StringBuffer sb = new StringBuffer();

        sb.append(this.getVersion(jsonObject));

        int len = sb.length();
        len = len / 2;
        if (len > 127) {
            sb.insert(0, HexUtils.intToHex(len, 4));
        } else {
            sb.insert(0, HexUtils.intToHex(len, 2));
        }
        sb.insert(0, this.getDataID());
        return sb.toString();
    }

}
